package com.example.PullGame;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.example.wonderfulradish.R;

/**
 * Created by 陈勇劲 on 2017/10/3.
 */

public class Menu implements PullState {
    public static float widthM,heightM;
    private Resources res;
    private Bitmap backgroundBitmap;
    private Bitmap[] startButtonBitmaps,endButtonBitmaps;
    private Rect backgroundRect,startButtonRect,endButtonRect;
    private PullDrawView mainActivity;


    private boolean[] isPress;
    public Menu(Resources res,PullDrawView mainActivity){

        this.mainActivity=mainActivity;
        this.res=res;
        load();//load各种资源的方法
    }

    public void load(){
        isPress=new boolean[2];
        //加载背景图片
        backgroundBitmap= BitmapFactory.decodeResource(res, R.drawable.youxi1_select_orangebj_png);
        int left,top,right,bottom;
        //适应不同屏幕
        if(Constant.SCREEN_HEIGHT>backgroundBitmap.getHeight()){//当屏幕高度大于背景图高度时
            top=(Constant.SCREEN_HEIGHT-backgroundBitmap.getHeight())/2;
            bottom=Constant.SCREEN_HEIGHT-(Constant.SCREEN_HEIGHT-backgroundBitmap.getHeight())/2;
            heightM=1;

        }else {
            top=0;
            heightM=Constant.SCREEN_HEIGHT*1.0f/backgroundBitmap.getHeight();
            bottom=Constant.SCREEN_HEIGHT;
        }
        if(Constant.SCREEN_WIDTH>backgroundBitmap.getWidth()){//当屏幕宽度大于背景图宽度时
            left=(Constant.SCREEN_WIDTH-backgroundBitmap.getWidth())/2;
            right=Constant.SCREEN_WIDTH-(Constant.SCREEN_WIDTH-backgroundBitmap.getWidth())/2;
            widthM=1;
        }else {
            left=0;
            widthM=Constant.SCREEN_WIDTH*1.0f/backgroundBitmap.getWidth();
            right=Constant.SCREEN_WIDTH;
        }
        //创建背景图片矩形块
              backgroundRect=new Rect(left, top, right, bottom);
        //backgroundRect=new Rect(bottom,left,top,right);


        //加载开始游戏按钮图片
        startButtonBitmaps=new Bitmap[2];
        startButtonBitmaps[0]=BitmapFactory.decodeResource(res, R.drawable.icon_back_83px);
        startButtonBitmaps[1]=BitmapFactory.decodeResource(res, R.drawable.button2);
        //创建开始游戏按钮矩形块
//		int startLeft=(int) ((Constant.SCREEN_WIDTH/2-startButtonBitmaps[0].getWidth()/2)*widthM);
//		int startTop=(int) ((Constant.SCREEN_HEIGHT/2)*heightM);
//		int startRight=(int) ((Constant.SCREEN_WIDTH/2+startButtonBitmaps[0].getWidth()/2)*widthM);
//		int startBottom=(int) ((Constant.SCREEN_HEIGHT/2+startButtonBitmaps[0].getHeight())*heightM);
        //将图片放置在屏幕中间
        startButtonRect=new Rect( Constant.SCREEN_WIDTH/2-startButtonBitmaps[0].getWidth()/2,
                Constant.SCREEN_HEIGHT/2,
                Constant.SCREEN_WIDTH/2+startButtonBitmaps[0].getWidth()/2,
                Constant.SCREEN_HEIGHT/2+startButtonBitmaps[0].getHeight());


//		startBottomRect=new Rect(startLeft, startTop, startRight, startBottom);
        //加载结束游戏按钮图片
        endButtonBitmaps=new Bitmap[2];
        endButtonBitmaps[0]=BitmapFactory.decodeResource(res, R.drawable.reback84);
        endButtonBitmaps[1]=BitmapFactory.decodeResource(res, R.drawable.button2);
        //创建结束游戏按钮矩形块
       /* endButtonRect=new Rect(right-endButtonBitmaps[0].getWidth()-10,
                bottom-endButtonBitmaps[0].getHeight()-10,
                right-10,
                bottom-10);*/
        endButtonRect=new Rect(
                Constant.SCREEN_WIDTH/9,
                Constant.SCREEN_HEIGHT/12,
                Constant.SCREEN_WIDTH/9+endButtonBitmaps[0].getWidth(),
                Constant.SCREEN_HEIGHT/12+endButtonBitmaps[0].getHeight());
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        // 画背景
        canvas.drawBitmap(backgroundBitmap, null, backgroundRect, paint);
        // 根据是否按下条件画开始按钮
        if(!isPress[0]){
            canvas.drawBitmap(startButtonBitmaps[0], null, startButtonRect, paint);
        }else{
            canvas.drawBitmap(startButtonBitmaps[1], null, startButtonRect, paint);
        }
        // 根据是否按下条件画开始按钮
        if(!isPress[1]){
            canvas.drawBitmap(endButtonBitmaps[0], null, endButtonRect, paint);
        }else{
            canvas.drawBitmap(endButtonBitmaps[1], null, endButtonRect, paint);
        }
    }

    @Override
    public void touch(MotionEvent event) {
        int x=(int) event.getX();//获得触摸点X坐标
        int y=(int) event.getY();//获得触摸点y坐标
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(startButtonRect.contains(x, y)){  //如果触摸点在开始按钮的矩形块内
                    isPress[0]=true; //开始按下为true
                }
                if(endButtonRect.contains(x, y)){//如果触摸点在结束按钮的矩形块内
                    isPress[1]=true;//结束按下为true
                }
                break;

            case MotionEvent.ACTION_UP:
                if(startButtonRect.contains(x, y)){

                    PullGameView.pullState=new PullGamePlay(res, this,mainActivity); //触摸抬起时，进入到游戏画面
                }
                if(endButtonRect.contains(x, y)){
                    System.exit(0);			//触摸抬起时，退出游戏
                }
                isPress[0]=false;
                isPress[1]=false;
                break;
        }
    }
    public Rect getBackgroundRect() {  //得到背景框大小的方法
        return backgroundRect;
    }

}

